<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>registry.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_tweak_registry.js.html">registry.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Definition for goog.tweak.Registry.
<a name="line17"></a> * Most clients should not use this class directly, but instead use the API
<a name="line18"></a> * defined in tweak.js. One possible use case for directly using TweakRegistry
<a name="line19"></a> * is to register tweaks that are not known at compile time.
<a name="line20"></a> *
<a name="line21"></a> * @author agrieve@google.com (Andrew Grieve)
<a name="line22"></a> */
<a name="line23"></a>
<a name="line24"></a>goog.provide(&#39;goog.tweak.Registry&#39;);
<a name="line25"></a>
<a name="line26"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line27"></a>goog.require(&#39;goog.debug.Logger&#39;);
<a name="line28"></a>goog.require(&#39;goog.object&#39;);
<a name="line29"></a>goog.require(&#39;goog.string&#39;);
<a name="line30"></a>goog.require(&#39;goog.tweak.BaseEntry&#39;);
<a name="line31"></a>goog.require(&#39;goog.uri.utils&#39;);
<a name="line32"></a>
<a name="line33"></a>
<a name="line34"></a>
<a name="line35"></a>/**
<a name="line36"></a> * Singleton that manages all tweaks. This should be instantiated only from
<a name="line37"></a> * goog.tweak.getRegistry().
<a name="line38"></a> * @param {string} queryParams Value of window.location.search.
<a name="line39"></a> * @param {!Object.&lt;string|number|boolean&gt;} compilerOverrides Default value
<a name="line40"></a> *     overrides set by the compiler.
<a name="line41"></a> * @constructor
<a name="line42"></a> */
<a name="line43"></a>goog.tweak.Registry = function(queryParams, compilerOverrides) {
<a name="line44"></a>  /**
<a name="line45"></a>   * A map of entry id -&gt; entry object
<a name="line46"></a>   * @type {!Object.&lt;!goog.tweak.BaseEntry&gt;}
<a name="line47"></a>   * @private
<a name="line48"></a>   */
<a name="line49"></a>  this.entryMap_ = {};
<a name="line50"></a>
<a name="line51"></a>  /**
<a name="line52"></a>   * The map of query params to use when initializing entry settings.
<a name="line53"></a>   * @type {!Object.&lt;string&gt;}
<a name="line54"></a>   * @private
<a name="line55"></a>   */
<a name="line56"></a>  this.parsedQueryParams_ = goog.tweak.Registry.parseQueryParams(queryParams);
<a name="line57"></a>
<a name="line58"></a>  /**
<a name="line59"></a>   * List of callbacks to call when a new entry is registered.
<a name="line60"></a>   * @type {!Array.&lt;!Function&gt;}
<a name="line61"></a>   * @private
<a name="line62"></a>   */
<a name="line63"></a>  this.onRegisterListeners_ = [];
<a name="line64"></a>
<a name="line65"></a>  /**
<a name="line66"></a>   * A map of entry ID -&gt; default value override for overrides set by the
<a name="line67"></a>   * compiler.
<a name="line68"></a>   * @type {!Object.&lt;string|number|boolean&gt;}
<a name="line69"></a>   * @private
<a name="line70"></a>   */
<a name="line71"></a>  this.compilerDefaultValueOverrides_ = compilerOverrides;
<a name="line72"></a>
<a name="line73"></a>  /**
<a name="line74"></a>   * A map of entry ID -&gt; default value override for overrides set by
<a name="line75"></a>   * goog.tweak.overrideDefaultValue().
<a name="line76"></a>   * @type {!Object.&lt;string|number|boolean&gt;}
<a name="line77"></a>   * @private
<a name="line78"></a>   */
<a name="line79"></a>  this.defaultValueOverrides_ = {};
<a name="line80"></a>};
<a name="line81"></a>
<a name="line82"></a>
<a name="line83"></a>/**
<a name="line84"></a> * The logger for this class.
<a name="line85"></a> * @type {!goog.debug.Logger}
<a name="line86"></a> * @private
<a name="line87"></a> */
<a name="line88"></a>goog.tweak.Registry.prototype.logger_ =
<a name="line89"></a>    goog.debug.Logger.getLogger(&#39;goog.tweak.Registry&#39;);
<a name="line90"></a>
<a name="line91"></a>
<a name="line92"></a>/**
<a name="line93"></a> * Simple parser for query params. Makes all keys lower-case.
<a name="line94"></a> * @param {string} queryParams The part of the url between the ? and the #.
<a name="line95"></a> * @return {!Object.&lt;string&gt;} map of key-&gt;value.
<a name="line96"></a> */
<a name="line97"></a>goog.tweak.Registry.parseQueryParams = function(queryParams) {
<a name="line98"></a>  // Strip off the leading ? and split on &amp;.
<a name="line99"></a>  var parts = queryParams.substr(1).split(&#39;&amp;&#39;);
<a name="line100"></a>  var ret = {};
<a name="line101"></a>
<a name="line102"></a>  for (var i = 0, il = parts.length; i &lt; il; ++i) {
<a name="line103"></a>    var entry = parts[i].split(&#39;=&#39;);
<a name="line104"></a>    if (entry[0]) {
<a name="line105"></a>      ret[goog.string.urlDecode(entry[0]).toLowerCase()] =
<a name="line106"></a>          goog.string.urlDecode(entry[1] || &#39;&#39;);
<a name="line107"></a>    }
<a name="line108"></a>  }
<a name="line109"></a>  return ret;
<a name="line110"></a>};
<a name="line111"></a>
<a name="line112"></a>
<a name="line113"></a>/**
<a name="line114"></a> * Registers the given tweak setting/action.
<a name="line115"></a> * @param {goog.tweak.BaseEntry} entry The entry.
<a name="line116"></a> */
<a name="line117"></a>goog.tweak.Registry.prototype.register = function(entry) {
<a name="line118"></a>  var id = entry.getId();
<a name="line119"></a>  var oldBaseEntry = this.entryMap_[id];
<a name="line120"></a>  if (oldBaseEntry) {
<a name="line121"></a>    if (oldBaseEntry == entry) {
<a name="line122"></a>      this.logger_.warning(&#39;Tweak entry registered twice: &#39; + id);
<a name="line123"></a>      return;
<a name="line124"></a>    }
<a name="line125"></a>    goog.asserts.fail(
<a name="line126"></a>        &#39;Tweak entry registered twice and with different types: &#39; + id);
<a name="line127"></a>  }
<a name="line128"></a>
<a name="line129"></a>  // Check for a default value override, either from compiler flags or from a
<a name="line130"></a>  // call to overrideDefaultValue().
<a name="line131"></a>  var defaultValueOverride = (id in this.compilerDefaultValueOverrides_) ?
<a name="line132"></a>      this.compilerDefaultValueOverrides_[id] : this.defaultValueOverrides_[id];
<a name="line133"></a>  if (goog.isDef(defaultValueOverride)) {
<a name="line134"></a>    goog.asserts.assertInstanceof(entry, goog.tweak.BasePrimitiveSetting,
<a name="line135"></a>        &#39;Cannot set the default value of non-primitive setting %s&#39;,
<a name="line136"></a>        entry.label);
<a name="line137"></a>    entry.setDefaultValue(defaultValueOverride);
<a name="line138"></a>  }
<a name="line139"></a>
<a name="line140"></a>  // Set its value from the query params.
<a name="line141"></a>  if (entry instanceof goog.tweak.BaseSetting) {
<a name="line142"></a>    if (entry.getParamName()) {
<a name="line143"></a>      entry.setInitialQueryParamValue(
<a name="line144"></a>          this.parsedQueryParams_[entry.getParamName()]);
<a name="line145"></a>    }
<a name="line146"></a>  }
<a name="line147"></a>
<a name="line148"></a>  this.entryMap_[id] = entry;
<a name="line149"></a>  // Call all listeners.
<a name="line150"></a>  for (var i = 0, callback; callback = this.onRegisterListeners_[i]; ++i) {
<a name="line151"></a>    callback(entry);
<a name="line152"></a>  }
<a name="line153"></a>};
<a name="line154"></a>
<a name="line155"></a>
<a name="line156"></a>/**
<a name="line157"></a> * Adds a callback to be called whenever a new tweak is added.
<a name="line158"></a> * @param {!Function} func The callback.
<a name="line159"></a> */
<a name="line160"></a>goog.tweak.Registry.prototype.addOnRegisterListener = function(func) {
<a name="line161"></a>  this.onRegisterListeners_.push(func);
<a name="line162"></a>};
<a name="line163"></a>
<a name="line164"></a>
<a name="line165"></a>/**
<a name="line166"></a> * @param {string} id The unique string that identifies this entry.
<a name="line167"></a> * @return {boolean} Whether a tweak with the given ID is registered.
<a name="line168"></a> */
<a name="line169"></a>goog.tweak.Registry.prototype.hasEntry = function(id) {
<a name="line170"></a>  return id in this.entryMap_;
<a name="line171"></a>};
<a name="line172"></a>
<a name="line173"></a>
<a name="line174"></a>/**
<a name="line175"></a> * Returns the BaseEntry with the given ID. Asserts if it does not exists.
<a name="line176"></a> * @param {string} id The unique string that identifies this entry.
<a name="line177"></a> * @return {!goog.tweak.BaseEntry} The entry.
<a name="line178"></a> */
<a name="line179"></a>goog.tweak.Registry.prototype.getEntry = function(id) {
<a name="line180"></a>  var ret = this.entryMap_[id];
<a name="line181"></a>  goog.asserts.assert(ret, &#39;Tweak not registered: %s&#39;, id);
<a name="line182"></a>  return ret;
<a name="line183"></a>};
<a name="line184"></a>
<a name="line185"></a>
<a name="line186"></a>/**
<a name="line187"></a> * Returns the boolean setting with the given ID. Asserts if the ID does not
<a name="line188"></a> * refer to a registered entry or if it refers to one of the wrong type.
<a name="line189"></a> * @param {string} id The unique string that identifies this entry.
<a name="line190"></a> * @return {!goog.tweak.BooleanSetting} The entry.
<a name="line191"></a> */
<a name="line192"></a>goog.tweak.Registry.prototype.getBooleanSetting = function(id) {
<a name="line193"></a>  var entry = this.getEntry(id);
<a name="line194"></a>  goog.asserts.assertInstanceof(entry, goog.tweak.BooleanSetting,
<a name="line195"></a>      &#39;getBooleanSetting called on wrong type of BaseSetting&#39;);
<a name="line196"></a>  return /** @type {!goog.tweak.BooleanSetting} */ (entry);
<a name="line197"></a>};
<a name="line198"></a>
<a name="line199"></a>
<a name="line200"></a>/**
<a name="line201"></a> * Returns the string setting with the given ID. Asserts if the ID does not
<a name="line202"></a> * refer to a registered entry or if it refers to one of the wrong type.
<a name="line203"></a> * @param {string} id The unique string that identifies this entry.
<a name="line204"></a> * @return {!goog.tweak.StringSetting} The entry.
<a name="line205"></a> */
<a name="line206"></a>goog.tweak.Registry.prototype.getStringSetting = function(id) {
<a name="line207"></a>  var entry = this.getEntry(id);
<a name="line208"></a>  goog.asserts.assertInstanceof(entry, goog.tweak.StringSetting,
<a name="line209"></a>      &#39;getStringSetting called on wrong type of BaseSetting&#39;);
<a name="line210"></a>  return /** @type {!goog.tweak.StringSetting} */ (entry);
<a name="line211"></a>};
<a name="line212"></a>
<a name="line213"></a>
<a name="line214"></a>/**
<a name="line215"></a> * Returns the numeric setting with the given ID. Asserts if the ID does not
<a name="line216"></a> * refer to a registered entry or if it refers to one of the wrong type.
<a name="line217"></a> * @param {string} id The unique string that identifies this entry.
<a name="line218"></a> * @return {!goog.tweak.NumericSetting} The entry.
<a name="line219"></a> */
<a name="line220"></a>goog.tweak.Registry.prototype.getNumericSetting = function(id) {
<a name="line221"></a>  var entry = this.getEntry(id);
<a name="line222"></a>  goog.asserts.assertInstanceof(entry, goog.tweak.NumericSetting,
<a name="line223"></a>      &#39;getNumericSetting called on wrong type of BaseSetting&#39;);
<a name="line224"></a>  return /** @type {!goog.tweak.NumericSetting} */ (entry);
<a name="line225"></a>};
<a name="line226"></a>
<a name="line227"></a>
<a name="line228"></a>/**
<a name="line229"></a> * Creates and returns an array of all BaseSetting objects with an associted
<a name="line230"></a> * query parameter.
<a name="line231"></a> * @param {boolean} excludeChildEntries Exclude BooleanInGroupSettings.
<a name="line232"></a> * @param {boolean} excludeNonSettings Exclude entries that are not subclasses
<a name="line233"></a> *     of BaseSetting.
<a name="line234"></a> * @return {!Array.&lt;!goog.tweak.BaseSetting&gt;} The settings.
<a name="line235"></a> */
<a name="line236"></a>goog.tweak.Registry.prototype.extractEntries =
<a name="line237"></a>    function(excludeChildEntries, excludeNonSettings) {
<a name="line238"></a>  var entries = [];
<a name="line239"></a>  for (var id in this.entryMap_) {
<a name="line240"></a>    var entry = this.entryMap_[id];
<a name="line241"></a>    if (entry instanceof goog.tweak.BaseSetting) {
<a name="line242"></a>      if (excludeChildEntries &amp;&amp; !entry.getParamName()) {
<a name="line243"></a>        continue;
<a name="line244"></a>      }
<a name="line245"></a>    } else if (excludeNonSettings) {
<a name="line246"></a>      continue;
<a name="line247"></a>    }
<a name="line248"></a>    entries.push(entry);
<a name="line249"></a>  }
<a name="line250"></a>  return entries;
<a name="line251"></a>};
<a name="line252"></a>
<a name="line253"></a>
<a name="line254"></a>/**
<a name="line255"></a> * Returns the query part of the URL that will apply all set tweaks.
<a name="line256"></a> * @param {string=} opt_existingSearchStr The part of the url between the ? and
<a name="line257"></a> *     the #. Uses window.location.search if not given.
<a name="line258"></a> * @return {string} The query string.
<a name="line259"></a> */
<a name="line260"></a>goog.tweak.Registry.prototype.makeUrlQuery =
<a name="line261"></a>    function(opt_existingSearchStr) {
<a name="line262"></a>  var existingParams = opt_existingSearchStr == undefined ?
<a name="line263"></a>      window.location.search : opt_existingSearchStr;
<a name="line264"></a>
<a name="line265"></a>  var sortedEntries = this.extractEntries(true /* excludeChildEntries */,
<a name="line266"></a>                                          true /* excludeNonSettings */);
<a name="line267"></a>  // Sort the params so that the urlQuery has stable ordering.
<a name="line268"></a>  sortedEntries.sort(function(a, b) {
<a name="line269"></a>    return goog.array.defaultCompare(a.getParamName(), b.getParamName());
<a name="line270"></a>  });
<a name="line271"></a>
<a name="line272"></a>  // Add all values that are not set to their defaults.
<a name="line273"></a>  var keysAndValues = [];
<a name="line274"></a>  for (var i = 0, entry; entry = sortedEntries[i]; ++i) {
<a name="line275"></a>    var encodedValue = entry.getNewValueEncoded();
<a name="line276"></a>    if (encodedValue != null) {
<a name="line277"></a>      keysAndValues.push(entry.getParamName(), encodedValue);
<a name="line278"></a>    }
<a name="line279"></a>    // Strip all tweak query params from the existing query string. This will
<a name="line280"></a>    // make the final query string contain only the tweak settings that are set
<a name="line281"></a>    // to their non-default values and also maintain non-tweak related query
<a name="line282"></a>    // parameters.
<a name="line283"></a>    existingParams = goog.uri.utils.removeParam(existingParams,
<a name="line284"></a>        encodeURIComponent(/** @type {string} */ (entry.getParamName())));
<a name="line285"></a>  }
<a name="line286"></a>
<a name="line287"></a>  var tweakParams = goog.uri.utils.buildQueryData(keysAndValues);
<a name="line288"></a>  // Decode spaces and commas in order to make the URL more readable.
<a name="line289"></a>  tweakParams = tweakParams.replace(/%2C/g, &#39;,&#39;).replace(/%20/g, &#39;+&#39;);
<a name="line290"></a>  return !tweakParams ? existingParams :
<a name="line291"></a>      existingParams ? existingParams + &#39;&amp;&#39; + tweakParams :
<a name="line292"></a>      &#39;?&#39; + tweakParams;
<a name="line293"></a>};
<a name="line294"></a>
<a name="line295"></a>
<a name="line296"></a>/**
<a name="line297"></a> * Sets a default value to use for the given tweak instead of the one passed
<a name="line298"></a> * to the register* function. This function must be called before the tweak is
<a name="line299"></a> * registered.
<a name="line300"></a> * @param {string} id The unique string that identifies the entry.
<a name="line301"></a> * @param {string|number|boolean} value The replacement value to be used as the
<a name="line302"></a> *     default value for the setting.
<a name="line303"></a> */
<a name="line304"></a>goog.tweak.Registry.prototype.overrideDefaultValue = function(id, value) {
<a name="line305"></a>  goog.asserts.assert(!this.hasEntry(id),
<a name="line306"></a>      &#39;goog.tweak.overrideDefaultValue must be called before the tweak is &#39; +
<a name="line307"></a>      &#39;registered. Tweak: %s&#39;, id);
<a name="line308"></a>  this.defaultValueOverrides_[id] = value;
<a name="line309"></a>};
</pre>


</body>
</html>
